import request from '../../../utils/request'

Page({
        data: {
                doctorlist: [],
                chatList: []
        },
        async onLoad() {
                let t = this
                // 可在此处添加数据加载逻辑（如从接口获取）
                await t.getchatlist()
                await t.getdoctorlist()

        },
        getchatlist() {
                let t = this
                return new Promise((resolve, reject) => {
                        request({
                                url: 'chat/chat/chatlist',
                                method: 'get',
                                data: {
                                        receiveId: wx.getStorageSync('userid')
                                },
                                success(res) {
                                        console.log('chatlist', res.data.data);
                                        t.setData({
                                                doctorlist: res.data.data
                                        }, () => {
                                                resolve(1)
                                        })

                                }

                        })
                })
        },
        getdoctorlist() {
                let t = this
                return new Promise((resolve, reject) => {
                        t.data.doctorlist.forEach(async item => {
                                await t.getdoctordetail(item)
                                resolve(1)
                        })
                })
        },
        getdoctordetail(id) {
                let t = this
                return new Promise((resolve, reject) => {
                        request({
                                url: 'system/doctor/' + id,
                                method: 'get',
                                success(res) {
                                        console.log('doctordetail', res.data.data);
                                        t.setData({
                                                chatList: [...t.data.chatList, {
                                                        id:res.data.data.id,
                                                        name: res.data.data.name,
                                                        avatar: res.data.data.avator
                                                }]
                                        }, () => {
                                                resolve(1)
                                        })

                                }
                        })
                })
        },
        tochat(e){
console.log(e.currentTarget.dataset.item);
wx.navigateTo({
  url: '/pages/message/chat/chatdetail/chatdetail',
  success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', e.currentTarget.dataset.item)
      }

})
        }

})